package com.example.demo.quant.impl;

import com.example.demo.entity.RaceLog;
import com.example.demo.mapper.RaceLogMapper;
import com.example.demo.quant.DataMetric;
import com.example.demo.quant.QuantContext;
import com.example.demo.quant.QuantRace;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

@Slf4j
@AllArgsConstructor
@Getter
public class QuantContextImpl implements QuantContext {
    private QuantRace quantRace;

    private RaceLogMapper raceLogMapper;

    @Override
    public void buy(int count, DataMetric metric) {
        if (quantRace.getBalance() - (count * metric.getPrice()) < 0) {
            log.warn("#25 买不了 没钱了, count=" + count + ", metric=" + metric + ", quantRace=" + quantRace);
            return;
        }

        quantRace.setStock(quantRace.getStock() + 100);
        quantRace.setBalance(quantRace.getBalance() - (count * metric.getPrice()));
        log.warn("#10 买入 " + count + ", metric=" + metric + ", quantRace=" + quantRace);

        raceLogMapper.insert(new RaceLog()
                .setRaceId(quantRace.getRaceId())
                .setPrice(metric.getPrice())
                .setVolume(metric.getVolume())
                .setStock(quantRace.getStock())
                .setBalance(quantRace.getBalance())
                .setDescription("#10 买入 " + count + ", metric=" + metric + ", quantRace=" + quantRace)
                .setCreatedAt(LocalDateTime.now()));
    }

    @Override
    public void sell(int count, DataMetric metric) {
        if (quantRace.getStock() < count) {
            log.warn("#41 卖不了 没货了, count=" + count + ", metric=" + metric + ", quantRace=" + quantRace);
            return;
        }

        quantRace.setStock(quantRace.getStock() - count);
        quantRace.setBalance(quantRace.getBalance() + (count * metric.getPrice()));
        log.warn("#15 卖出, count=" + count + ", metric=" + metric + ", quantRace=" + quantRace);

        raceLogMapper.insert(new RaceLog()
                .setRaceId(quantRace.getRaceId())
                .setPrice(metric.getPrice())
                .setVolume(metric.getVolume())
                .setStock(quantRace.getStock())
                .setBalance(quantRace.getBalance())
                .setDescription("#15 卖出 " + count + ", metric=" + metric + ", quantRace=" + quantRace)
                .setCreatedAt(LocalDateTime.now()));
    }

    @Override
    public void keep(DataMetric metric) {
        log.warn("#20 不动" + ", metric=" + metric + ", quantRace=" + quantRace);

        raceLogMapper.insert(new RaceLog()
                .setRaceId(quantRace.getRaceId())
                .setPrice(metric.getPrice())
                .setVolume(metric.getVolume())
                .setStock(quantRace.getStock())
                .setBalance(quantRace.getBalance())
                .setDescription("#20 不动" + ", metric=" + metric + ", quantRace=" + quantRace)
                .setCreatedAt(LocalDateTime.now()));
    }
}
